/*
 * Author: Kishore Reddy
 * Url: http://commonlibrarynet.codeplex.com/
 * Title: CommonLibrary.NET
 * Copyright: � 2009 Kishore Reddy
 * License: LGPL License
 * LicenseUrl: http://commonlibrarynet.codeplex.com/license
 * Description: A C# based .NET 3.5 Open-Source collection of reusable components.
 * Usage: Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using ComLib.Entities;
using ComLib.Data;
using ComLib.LocationSupport;



namespace ComLib.Web.Modules.Widgets
{
    /// <summary>
    /// Generic repository for persisting WidgetInstance.
    /// </summary>
    public partial class WidgetInstanceRepository : RepositorySql<WidgetInstance>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NamedQueryRepository"/> class.
        /// </summary>
        public WidgetInstanceRepository() { }

        
        /// <summary>
        /// Initializes a new instance of the <see cref="Repository&lt;TId, T&gt;"/> class.
        /// </summary>
        /// <param name="connectionInfo">The connection string.</param>
        public  WidgetInstanceRepository(string connectionString) : base(connectionString)
        {
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="Repository&lt;TId, T&gt;"/> class.
        /// </summary>
        /// <param name="connectionInfo">The connection info.</param>
        /// <param name="helper">The helper.</param>
        public WidgetInstanceRepository(ConnectionInfo connectionInfo) : base(connectionInfo)
        {            
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="Repository&lt;TId, T&gt;"/> class.
        /// </summary>
        /// <param name="connectionInfo">The connection info.</param>
        /// <param name="helper">The helper.</param>
        public WidgetInstanceRepository(ConnectionInfo connectionInfo, IDatabase db)
            : base(connectionInfo, db)
        {
        }


        /// <summary>
        /// Initialize the rowmapper
        /// </summary>
        public override void Init(ConnectionInfo connectionInfo, IDatabase db)
        {
            base.Init(connectionInfo, db);
            this.RowMapper = new WidgetInstanceRowMapper();
        }


        /// <summary>
        /// Create the entity using sql.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override WidgetInstance Create(WidgetInstance entity)
        {
            string sql = "insert into WidgetInstances ( [CreateDate], [UpdateDate], [CreateUser], [UpdateUser], [UpdateComment], [AppId]"
			 + ", [WidgetId], [WidgetLength], [Header], [Zone], [Group], [DefName]"
			 + ", [Roles], [Args], [SortIndex], [MaxRecords], [IsActive], [IsPublic]"
			 + ", [IsHighlighted], [IsRolesBased], [RefModel], [RefName], [RefGroup], [RefEmail]"
			 + ", [RefUrl], [RefSource], [RefKey], [RefContent], [RefId1], [RefId2]"
			 + ", [RefDate1], [RefDate2], [RefFlag1], [RefFlag2], [RefInt1], [RefInt2] ) values ( @CreateDate, @UpdateDate, @CreateUser, @UpdateUser, @UpdateComment, @AppId"
			 + ", @WidgetId, @WidgetLength, @Header, @Zone, @Group, @DefName"
			 + ", @Roles, @Args, @SortIndex, @MaxRecords, @IsActive, @IsPublic"
			 + ", @IsHighlighted, @IsRolesBased, @RefModel, @RefName, @RefGroup, @RefEmail"
			 + ", @RefUrl, @RefSource, @RefKey, @RefContent, @RefId1, @RefId2"
			 + ", @RefDate1, @RefDate2, @RefFlag1, @RefFlag2, @RefInt1, @RefInt2 );" + IdentityStatement;;
            var dbparams = BuildParams(entity);            
            object result = _db.ExecuteScalarText(sql, dbparams);
            entity.Id = Convert.ToInt32(result);
            return entity;
        }


        /// <summary>
        /// Update the entity using sql.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override WidgetInstance Update(WidgetInstance entity)
        {
            string sql = "update WidgetInstances set [CreateDate] = @CreateDate, [UpdateDate] = @UpdateDate, [CreateUser] = @CreateUser, [UpdateUser] = @UpdateUser, [UpdateComment] = @UpdateComment, [AppId] = @AppId"
			 + ", [WidgetId] = @WidgetId, [WidgetLength] = @WidgetLength, [Header] = @Header, [Zone] = @Zone, [Group] = @Group, [DefName] = @DefName"
			 + ", [Roles] = @Roles, [Args] = @Args, [SortIndex] = @SortIndex, [MaxRecords] = @MaxRecords, [IsActive] = @IsActive, [IsPublic] = @IsPublic"
			 + ", [IsHighlighted] = @IsHighlighted, [IsRolesBased] = @IsRolesBased, [RefModel] = @RefModel, [RefName] = @RefName, [RefGroup] = @RefGroup, [RefEmail] = @RefEmail"
			 + ", [RefUrl] = @RefUrl, [RefSource] = @RefSource, [RefKey] = @RefKey, [RefContent] = @RefContent, [RefId1] = @RefId1, [RefId2] = @RefId2"
			 + ", [RefDate1] = @RefDate1, [RefDate2] = @RefDate2, [RefFlag1] = @RefFlag1, [RefFlag2] = @RefFlag2, [RefInt1] = @RefInt1, [RefInt2] = @RefInt2 where Id = " + entity.Id;
            var dbparams = BuildParams(entity); 
            _db.ExecuteNonQueryText(sql, dbparams);
            return entity;
        }


        public override WidgetInstance Get(int id)
        {
            WidgetInstance entity = base.Get(id);
            
            return entity;
        }


        protected virtual DbParameter[] BuildParams(WidgetInstance entity)
        {
            var dbparams = new List<DbParameter>();
            			dbparams.Add(BuildParam("@CreateDate", SqlDbType.DateTime, entity.CreateDate));
			dbparams.Add(BuildParam("@UpdateDate", SqlDbType.DateTime, entity.UpdateDate));
			dbparams.Add(BuildParam("@CreateUser", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.CreateUser) ? "" : entity.CreateUser));
			dbparams.Add(BuildParam("@UpdateUser", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.UpdateUser) ? "" : entity.UpdateUser));
			dbparams.Add(BuildParam("@UpdateComment", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.UpdateComment) ? "" : entity.UpdateComment));
			dbparams.Add(BuildParam("@AppId", SqlDbType.Int, entity.AppId));
			dbparams.Add(BuildParam("@WidgetId", SqlDbType.Int, entity.WidgetId));
			dbparams.Add(BuildParam("@WidgetLength", SqlDbType.Int, entity.WidgetLength));
			dbparams.Add(BuildParam("@Header", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.Header) ? "" : entity.Header));
			dbparams.Add(BuildParam("@Zone", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.Zone) ? "" : entity.Zone));
			dbparams.Add(BuildParam("@Group", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.Group) ? "" : entity.Group));
			dbparams.Add(BuildParam("@DefName", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.DefName) ? "" : entity.DefName));
			dbparams.Add(BuildParam("@Roles", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.Roles) ? "" : entity.Roles));
			dbparams.Add(BuildParam("@Args", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.Args) ? "" : entity.Args));
			dbparams.Add(BuildParam("@SortIndex", SqlDbType.Int, entity.SortIndex));
			dbparams.Add(BuildParam("@MaxRecords", SqlDbType.Int, entity.MaxRecords));
			dbparams.Add(BuildParam("@IsActive", SqlDbType.Bit, entity.IsActive));
			dbparams.Add(BuildParam("@IsPublic", SqlDbType.Bit, entity.IsPublic));
			dbparams.Add(BuildParam("@IsHighlighted", SqlDbType.Bit, entity.IsHighlighted));
			dbparams.Add(BuildParam("@IsRolesBased", SqlDbType.Bit, entity.IsRolesBased));
			dbparams.Add(BuildParam("@RefModel", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefModel) ? "" : entity.RefModel));
			dbparams.Add(BuildParam("@RefName", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefName) ? "" : entity.RefName));
			dbparams.Add(BuildParam("@RefGroup", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefGroup) ? "" : entity.RefGroup));
			dbparams.Add(BuildParam("@RefEmail", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefEmail) ? "" : entity.RefEmail));
			dbparams.Add(BuildParam("@RefUrl", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefUrl) ? "" : entity.RefUrl));
			dbparams.Add(BuildParam("@RefSource", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefSource) ? "" : entity.RefSource));
			dbparams.Add(BuildParam("@RefKey", SqlDbType.NVarChar, string.IsNullOrEmpty(entity.RefKey) ? "" : entity.RefKey));
			dbparams.Add(BuildParam("@RefContent", SqlDbType.NText, string.IsNullOrEmpty(entity.RefContent) ? "" : entity.RefContent));
			dbparams.Add(BuildParam("@RefId1", SqlDbType.Int, entity.RefId1));
			dbparams.Add(BuildParam("@RefId2", SqlDbType.Int, entity.RefId2));
			dbparams.Add(BuildParam("@RefDate1", SqlDbType.DateTime, entity.RefDate1));
			dbparams.Add(BuildParam("@RefDate2", SqlDbType.DateTime, entity.RefDate2));
			dbparams.Add(BuildParam("@RefFlag1", SqlDbType.Bit, entity.RefFlag1));
			dbparams.Add(BuildParam("@RefFlag2", SqlDbType.Bit, entity.RefFlag2));
			dbparams.Add(BuildParam("@RefInt1", SqlDbType.Int, entity.RefInt1));
			dbparams.Add(BuildParam("@RefInt2", SqlDbType.Int, entity.RefInt2));

            return dbparams.ToArray();
        }


        protected virtual DbParameter BuildParam(string name, SqlDbType dbType, object val)
        {
            var param = new SqlParameter(name, dbType);
            param.Value = val;
            return param;
        }

    }


    
    /// <summary>
    /// RowMapper for WidgetInstance.
    /// </summary>
    /// <typeparam name="?"></typeparam>
    public partial class WidgetInstanceRowMapper : EntityRowMapper<WidgetInstance>, IEntityRowMapper<WidgetInstance>
    {
        public override WidgetInstance MapRow(IDataReader reader, int rowNumber)
        {
            WidgetInstance entity = WidgetInstance.New();
            			entity.Id = reader["Id"] == DBNull.Value ? 0 : (int)reader["Id"];
			entity.CreateDate = reader["CreateDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["CreateDate"];
			entity.UpdateDate = reader["UpdateDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["UpdateDate"];
			entity.CreateUser = reader["CreateUser"] == DBNull.Value ? string.Empty : reader["CreateUser"].ToString();
			entity.UpdateUser = reader["UpdateUser"] == DBNull.Value ? string.Empty : reader["UpdateUser"].ToString();
			entity.UpdateComment = reader["UpdateComment"] == DBNull.Value ? string.Empty : reader["UpdateComment"].ToString();
			entity.AppId = reader["AppId"] == DBNull.Value ? 0 : (int)reader["AppId"];
			entity.WidgetId = reader["WidgetId"] == DBNull.Value ? 0 : (int)reader["WidgetId"];
			entity.WidgetLength = reader["WidgetLength"] == DBNull.Value ? 0 : (int)reader["WidgetLength"];
			entity.Header = reader["Header"] == DBNull.Value ? string.Empty : reader["Header"].ToString();
			entity.Zone = reader["Zone"] == DBNull.Value ? string.Empty : reader["Zone"].ToString();
			entity.Group = reader["Group"] == DBNull.Value ? string.Empty : reader["Group"].ToString();
			entity.DefName = reader["DefName"] == DBNull.Value ? string.Empty : reader["DefName"].ToString();
			entity.Roles = reader["Roles"] == DBNull.Value ? string.Empty : reader["Roles"].ToString();
			entity.Args = reader["Args"] == DBNull.Value ? string.Empty : reader["Args"].ToString();
			entity.SortIndex = reader["SortIndex"] == DBNull.Value ? 0 : (int)reader["SortIndex"];
			entity.MaxRecords = reader["MaxRecords"] == DBNull.Value ? 0 : (int)reader["MaxRecords"];
			entity.IsActive = reader["IsActive"] == DBNull.Value ? false : (bool)reader["IsActive"];
			entity.IsPublic = reader["IsPublic"] == DBNull.Value ? false : (bool)reader["IsPublic"];
			entity.IsHighlighted = reader["IsHighlighted"] == DBNull.Value ? false : (bool)reader["IsHighlighted"];
			entity.IsRolesBased = reader["IsRolesBased"] == DBNull.Value ? false : (bool)reader["IsRolesBased"];
			entity.RefModel = reader["RefModel"] == DBNull.Value ? string.Empty : reader["RefModel"].ToString();
			entity.RefName = reader["RefName"] == DBNull.Value ? string.Empty : reader["RefName"].ToString();
			entity.RefGroup = reader["RefGroup"] == DBNull.Value ? string.Empty : reader["RefGroup"].ToString();
			entity.RefEmail = reader["RefEmail"] == DBNull.Value ? string.Empty : reader["RefEmail"].ToString();
			entity.RefUrl = reader["RefUrl"] == DBNull.Value ? string.Empty : reader["RefUrl"].ToString();
			entity.RefSource = reader["RefSource"] == DBNull.Value ? string.Empty : reader["RefSource"].ToString();
			entity.RefKey = reader["RefKey"] == DBNull.Value ? string.Empty : reader["RefKey"].ToString();
			entity.RefContent = reader["RefContent"] == DBNull.Value ? string.Empty : reader["RefContent"].ToString();
			entity.RefId1 = reader["RefId1"] == DBNull.Value ? 0 : (int)reader["RefId1"];
			entity.RefId2 = reader["RefId2"] == DBNull.Value ? 0 : (int)reader["RefId2"];
			entity.RefDate1 = reader["RefDate1"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["RefDate1"];
			entity.RefDate2 = reader["RefDate2"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["RefDate2"];
			entity.RefFlag1 = reader["RefFlag1"] == DBNull.Value ? false : (bool)reader["RefFlag1"];
			entity.RefFlag2 = reader["RefFlag2"] == DBNull.Value ? false : (bool)reader["RefFlag2"];
			entity.RefInt1 = reader["RefInt1"] == DBNull.Value ? 0 : (int)reader["RefInt1"];
			entity.RefInt2 = reader["RefInt2"] == DBNull.Value ? 0 : (int)reader["RefInt2"];

            return entity;
        }
    }
}